﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using OptixMath = OptixDotNet.Math.MathUtils;
using OptixDotNetUtils;

using SlimDX;

namespace InstantRadiosity
{
    public class D3DCamera : Camera
    {
        public CameraData CameraData;

        public D3DCamera( float fov, float aspectRatio, float near, float far )
        {
            CameraData = new CameraData();
            CameraData.Proj = Matrix.PerspectiveFovLH( fov * OptixMath.DegreesToRadians, aspectRatio, near, far );
            CameraData.View = Matrix.Identity;
            CameraData.ViewProj = CameraData.View * CameraData.Proj;
        }

        public override void BuildView()
        {
            base.BuildView();

            // Fill in the view matrix entries.

            float x = -Position.Dot( mRight );
            float y = -Position.Dot( mUp );
            float z = -Position.Dot( mLook );

            CameraData.View[ 0, 0 ] = mRight.X;
            CameraData.View[ 1, 0 ] = mRight.Y;
            CameraData.View[ 2, 0 ] = mRight.Z;
            CameraData.View[ 3, 0 ] = x;

            CameraData.View[ 0, 1 ] = mUp.X;
            CameraData.View[ 1, 1 ] = mUp.Y;
            CameraData.View[ 2, 1 ] = mUp.Z;
            CameraData.View[ 3, 1 ] = y;

            CameraData.View[ 0, 2 ] = mLook.X;
            CameraData.View[ 1, 2 ] = mLook.Y;
            CameraData.View[ 2, 2 ] = mLook.Z;
            CameraData.View[ 3, 2 ] = z;

            CameraData.View[ 0, 3 ] = 0.0f;
            CameraData.View[ 1, 3 ] = 0.0f;
            CameraData.View[ 2, 3 ] = 0.0f;
            CameraData.View[ 3, 3 ] = 1;

            CameraData.ViewProj = CameraData.View * CameraData.Proj;
        }
    }
}
